﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Methods
{
   public class listMembers : Api_BaseExecute
   {
      private new listMembersInput Input { get; set; }
      private listMembersOutput Output { get; set; }

      // -------------------------------------------------------------------
      // =============================================== constructors
      public listMembers() { }
      // ---------
      public listMembers( listMembersInput input ) { Input = input; }
      // =============================================== Execute

      public listMembersOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public listMembersOutput Execute( listMembersInput input )
      {
         Input = input;
         Output = new listMembersOutput( Input );
         ValidationIsOK = true;
         //------------------------- validate it
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #region ================================================================= API Calls
      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         try
         {
            string since = "";
            if ( Input.parms.since != DateTime.MinValue ) { since = apiHelper.FormatApiDateParm( Input.parms.since ); }
            string status = Input.parms.status.ToString();
            // ---------------------------------- rpc call -------------------------------------------
            x_listMember[] rpcResult = apiXmlRpc.api.listMembers(
               Input.parms.apikey, Input.parms.id, status, since, Input.parms.start, Input.parms.limit );
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "id", Input.parms.id );
            dict.Add( "status", Input.parms.status.ToString() );
            if ( Input.parms.since != DateTime.MinValue )
               dict.Add( "since", apiHelper.FormatApiDateParm( Input.parms.since ) );
            dict.Add( "start", Input.parms.start.ToString() );
            dict.Add( "limit", Input.parms.limit.ToString() );
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      #endregion

   }
}
